package ru.amse.baltijsky.javascheme.node;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * A convenience stub implementation of <tt>INode</tt>.
 */
public abstract class AbstractNode<T extends INode> implements INode<T> {
    /**
     * Parent (nesting) node.
     */
    private T parent = null;
    /**
     * Next node (in the sence of program flow).
     */
    private T next = null;
    /**
     * List of children of the node (nested constructions).
     */
    private final List<T> children = new ArrayList<T>();

    public boolean hasNext() {
        return next != null;
    }

    public T getNext() {
        return next;
    }

    /**
     * Sets link to the next node.
     *
     * @param nextNode the next node
     */
    public void setNext(T nextNode) {
        next = nextNode;
    }

    public boolean hasParent() {
        return parent != null;
    }

    public T getParent() {
        return parent;
    }

    /**
     * Sets the parent for the node.
     *
     * @param parentNode the parent node
     */
    protected void setParent(T parentNode) {
        parent = parentNode;
    }

    public boolean hasChildren() {
        return !children.isEmpty();
    }

    public Iterator<T> getChildren() {
        return Collections.unmodifiableCollection(children).iterator();
    }

    /**
     * Adds a child to the node.
     *
     * @param p the new child node
     */
    public void addChild(T p) {
        children.add(p);
    }
}
